package com.woniuxy.hrp.web;

//import com.woniuxy.hrp.Jwt.JwtUtil;
import com.woniuxy.hrp.entity.*;
import com.woniuxy.hrp.security.JwtUtil;
import com.woniuxy.hrp.service.*;
import com.woniuxy.hrp.unityhandler.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Transactional
@RestController
@CrossOrigin
@RequestMapping("/api/v1/pay")
public class PayController {
    @Autowired
    private ReservationService reservationService;
    @Autowired
    private ArrangeTimeService arrangeTimeService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private WalletoprService walletoprService;
    @Autowired
    PatientService patientService;
    @Autowired
    WalletService walletService;
    @Autowired
    PayRecordService payRecordService;
    @Autowired
    PayService payService;
    @Autowired
    private JwtUtil jwtUtil;
    @RequestMapping("/getorder")
    public ResponseEntity<List<Order>> GetOrder(@RequestBody RP rp, @RequestHeader String jwt){
        int patientid = (int) jwtUtil.getInfo(jwt, "id");
        ResponseEntity<List<Order>> result = new ResponseEntity<>();
        List<Reservation> daizhifu = reservationService.selectBypidandstatus(patientid,rp.getStatus());
        if (daizhifu!=null) {
            List<Order> orders = new ArrayList<>();
            for (Reservation reservation : daizhifu) {
                Order order = new Order();
                Patient patient = patientService.getById(patientid);
                Doctor doctor = doctorService.selectByDoctorId(rp.getDoctorid());
                ArrangeTime arrangeTime = arrangeTimeService.getbyid(reservation.getDoctorAndTime().getId());
                order.setStarttime(arrangeTime.getStartTime());
                order.setName(doctor.getName());
                order.setFee(doctor.getPosition().getFee());
//                order.setArrangeTime(arrangeTime);
//                order.setDoctor(doctor);
//                order.setPatient(patient);
                order.setAccount(patient.getAccount());
                order.setDoctorandtimeid(reservation.getDoctorAndTime().getId());
                orders.add(order);
            }
            result.setData(orders);
            result.setMsg("成功");
            result.setCode("200");
        }else{
            result.setMsg("失败");
            result.setCode("500");
        }
        return result;
    }
    @RequestMapping("/getnatorder")
    public ResponseEntity<List<NatOrder>> GetNatOrder(@RequestHeader String jwt){
        int id = (int) jwtUtil.getInfo(jwt, "id");
        String account = patientService.getById(id).getAccount();
        List<Reservation> reservations = payService.getNatBypid(id);
        List<NatOrder> natOrders = new ArrayList<>();
        for (Reservation reservation : reservations) {
            NatOrder natOrder = new NatOrder();
            reservation.setDatetime("核酸检测");
            Nat nat = payService.getByrid(reservation.getId());
            natOrder.setNat(nat);
            natOrder.setReservation(reservation);
            natOrder.setName(account);
            natOrders.add(natOrder);
        }
        ResponseEntity<List<NatOrder>> result = new ResponseEntity<>();
        result.setData(natOrders);
        result.setCode("200");
        result.setMsg("成功");
        return result;
    }
    @RequestMapping("/paybywallet")
    public ResponseEntity paybywallet(@RequestBody Order order, @RequestHeader String jwt){
        int patientid = (int) jwtUtil.getInfo(jwt, "id");
        Wallet byPatientId = walletService.getByPatientId(patientid);
        BigDecimal balance = byPatientId.getBalance();
        if(order.getFee().compareTo(balance)>0){
            //跳转到支付宝支付
            return ResponseEntity.FAIL;
        }else{
            byPatientId.setBalance(order.getFee().negate());
            Reservation selectbypidanddtid = reservationService.selectbypidanddtid("n", order.getDoctorandtimeid());
            selectbypidanddtid.setPayMentTypeId(2);
            selectbypidanddtid.setStatus("y");
            reservationService.update(selectbypidanddtid);
            walletService.updateWallet(byPatientId);
            PayRecord payRecord =new PayRecord();
            payRecord.setFee(order.getFee());
            Date dNow = new Date( );
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            payRecord.setDateTime(ft.format(dNow));
            payRecord.setWay("钱包");
            payRecord.setReservationId(selectbypidanddtid.getId());
            payRecordService.add(payRecord);
            Walletopr walletopr = new Walletopr();
            Date date = new Date( );
            SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            walletopr.setDatetime(ss.format(date));
            walletopr.setFee(order.getFee());
            walletopr.setStatus("y");
            walletopr.setWay("钱包");
            walletopr.setProject("预约挂号");
            walletopr.setWalletid(byPatientId.getId());
            walletopr.setOrderId(selectbypidanddtid.getId().toString());
            walletoprService.add(walletopr);
            return ResponseEntity.SUCCESS;
        }
    }
    @RequestMapping("/paymedicinebywallet")
    public ResponseEntity paymedicinebywallet(@RequestBody Order order,@RequestHeader String jwt){
        int id = (int)jwtUtil.getInfo(jwt, "id");
        Wallet byPatientId = walletService.getByPatientId(id);
        BigDecimal balance = byPatientId.getBalance();
        List<Integer> dpm = order.getDpm();
        BigDecimal totalfee= new BigDecimal(0);
        for (Integer integer : dpm) {
            BigDecimal fee = payService.getDPMbyID(integer).getMedicine().getFee();
            Integer num = payService.getDPMbyID(integer).getNum();
            BigDecimal multiply = fee.multiply(new BigDecimal(num));
            totalfee=totalfee.add(multiply);
        }
        if(totalfee.compareTo(balance)>0){
            //余额不足，返回失败跳转到支付宝
            return ResponseEntity.FAIL;
        }else{
            byPatientId.setBalance(totalfee.negate());
            walletService.updateWallet(byPatientId);
            for (Integer integer : dpm) {
                 payService.updateDPMstatusbyId(integer);
            }
            Walletopr walletopr = new Walletopr();
            Date date = new Date( );
            SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            walletopr.setDatetime(ss.format(date));
            walletopr.setFee(totalfee);
            walletopr.setStatus("y");
            walletopr.setWay("钱包");
            walletopr.setProject("药费");
            walletopr.setWalletid(byPatientId.getId());
            walletopr.setOrderId("524989");
            walletoprService.add(walletopr);
            return ResponseEntity.SUCCESS;
        }
    }
    @RequestMapping("/payDPIbywallet")
    public ResponseEntity payDPIbywallet(@RequestBody Order order,@RequestHeader String jwt){
        //获取用户余额
        int patientid = (int) jwtUtil.getInfo(jwt, "id");
        Wallet byPatientId = walletService.getByPatientId(patientid);
        BigDecimal balance = byPatientId.getBalance();
        //通过order获取检查项目
        List<Integer> dpi = order.getDpi();
        BigDecimal totalfee= new BigDecimal(0);
        for (Integer integer : dpi) {
            BigDecimal fee = payService.getDPIById(integer).getMitem().getFee();
            totalfee=totalfee.add(fee);
        }
        if(totalfee.compareTo(balance)>0){
            //余额不足，返回失败跳转到支付宝
            return ResponseEntity.FAIL;
        }else{
            byPatientId.setBalance(totalfee.negate());
            walletService.updateWallet(byPatientId);
            for (Integer integer : dpi) {
                payService.updateDPIstatusbyId(integer);
            }
            Walletopr walletopr = new Walletopr();
            Date date = new Date( );
            SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            walletopr.setDatetime(ss.format(date));
            walletopr.setFee(totalfee);
            walletopr.setStatus("y");
            walletopr.setWay("钱包");
            walletopr.setProject("项目检查");
            walletopr.setWalletid(byPatientId.getId());
            walletopr.setOrderId("524989");
            walletoprService.add(walletopr);
            return ResponseEntity.SUCCESS;
        }
    }
    @RequestMapping("/paynat")
    public ResponseEntity paynat(@RequestBody NatOrder natOrder,@RequestHeader String jwt){
        int patientid = (int) jwtUtil.getInfo(jwt, "id");
        Wallet wallet = walletService.getByPatientId(patientid);
        BigDecimal balance = wallet.getBalance();
        BigDecimal fee = natOrder.getNat().getFee();
        if(fee.compareTo(balance)>0) {
            //跳转到支付宝支付
            return ResponseEntity.FAIL;
        }else{
            wallet.setBalance(fee.negate());
            walletService.updateWallet(wallet);
            Reservation reservation = payService.getreservationbyid(natOrder.getReservation().getId());
            reservation.setStatus("y");
            reservationService.update(reservation);
            PayRecord payRecord =new PayRecord();
            payRecord.setFee(fee);
            Date dNow = new Date( );
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            payRecord.setDateTime(ft.format(dNow));
            payRecord.setWay("钱包");
            payRecord.setReservationId(reservation.getId());
            payRecordService.add(payRecord);
            Walletopr walletopr = new Walletopr();
            Date date = new Date( );
            SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            walletopr.setDatetime(ss.format(date));
            walletopr.setFee(fee);
            walletopr.setStatus("y");
            walletopr.setWay("钱包");
            walletopr.setProject("核酸检测");
            walletopr.setWalletid(wallet.getId());
            walletopr.setOrderId(reservation.getId().toString());
            walletoprService.add(walletopr);
            return ResponseEntity.SUCCESS;
        }
    }

}
